package net.ba.map
{
	import com.google.maps.LatLng;
	import com.google.maps.LatLngBounds;
	import com.google.maps.overlays.GroundOverlay;
	import com.google.maps.overlays.GroundOverlayOptions;
	
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import mx.core.Application;
	import mx.events.EffectEvent;
	
	import net.ba.models.TaxonName;
	
	public class GbifOverlay
	{
		
		private var gbifGroundOverlay:GroundOverlay;
		
		public function GbifOverlay()
		{
		}

		public function addGbifOverlay(taxon:TaxonName):void {
			Application.application.mapComp.gbifLoadingBox.visible=true;
			Application.application.mapComp.gbifDensityLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadGbifOverlayComplete);	
			//This part has to be improved. It need to set up the correct bbox for the coordinates specified in the GroundOverlay.
			Application.application.mapComp.gbifDensityLoader.load(new URLRequest("http://java.imastedev.com/geoserver/wms?FORMAT=image%2Fpng&SERVICE=WMS&REQUEST=GetMap" + 
			"&WIDTH=1000" + 
			"&HEIGHT=1000" + 
			"&VERSION=1.0.0&TILED=false&TRANSPARENT=true&SRS=EPSG%3A900913&BBOX=-20037508.34,-20037508.34,20037508.34,20037508.34" + 
			"&LAYERS=ba:gbif_density" + 
			"&STYLES=" + 
			"&cql_filter=taxon_id=" + taxon.id));
			
		}
		
		private function onLoadGbifOverlayComplete(e:Event):void {
			gbifGroundOverlay = new GroundOverlay(Application.application.mapComp.gbifDensityLoader,
									new LatLngBounds(new LatLng(-85.05,-179.78), new LatLng(85.05,179.78)), 
									new GroundOverlayOptions(
		                  			{name: "GbifGroundOverlay",
		                   			description: "GbifGroundOverlay"}));
		    Application.application.mapComp.gbifDensityLoader.alpha =0;
		    Application.application.mapComp.map.addOverlay(gbifGroundOverlay);
		    Application.application.mapComp.overlaysFadeIn.play([Application.application.mapComp.gbifDensityLoader]);
		    Application.application.mapComp.gbifLoadingBox.visible=false;
		}	
		
		public function removeGbifOverlay():void {
			Application.application.mapComp.overlaysFadeOut.addEventListener(EffectEvent.EFFECT_END, function(e:Event):void {Application.application.mapComp.map.removeOverlay(gbifGroundOverlay);});
			Application.application.mapComp.overlaysFadeOut.play([Application.application.mapComp.gbifDensityLoader]);
		}
							

	}
}